﻿Imports CrystalReportViewer
Imports org.codegen.win.controls.Grid

Namespace Forms.List
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Public Class frmClassMemberList
        Inherits frmBaseGrid
        Implements IStudentContainingGrid

#Region "Designer"



        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub

        Friend WithEvents UcListClassMember As ucClassMemberList

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Me.UcListClassMember = New IIC.ucClassMemberList()
            Me.pnlGrid.SuspendLayout()
            Me.SuspendLayout()
            '
            'pnlGrid
            '
            Me.pnlGrid.Controls.Add(Me.UcListClassMember)
            Me.pnlGrid.Location = New System.Drawing.Point(0, 53)
            Me.pnlGrid.Size = New System.Drawing.Size(760, 389)
            '
            'UcListClassMember
            '
            Me.UcListClassMember.Dock = System.Windows.Forms.DockStyle.Fill
            Me.UcListClassMember.Location = New System.Drawing.Point(0, 0)
            Me.UcListClassMember.Name = "UcListClassMember"
            Me.UcListClassMember.Size = New System.Drawing.Size(760, 389)
            Me.UcListClassMember.TabIndex = 0
            '
            'frmClassMemberList
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(760, 442)
            Me.Name = "frmClassMemberList"
            Me.ShowExcelButton = True
            Me.ShowSearch = True
            Me.Text = "Class Member List"
            Me.Controls.SetChildIndex(Me.pnlGrid, 0)
            Me.pnlGrid.ResumeLayout(False)
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region "Standard Code"

        Private _PeriodClass As PeriodClass

        Public Property PeriodClass As PeriodClass
            Get
                Return Me._PeriodClass
            End Get
            Set(ByVal value As PeriodClass)
                Me._PeriodClass = value
                CType(Me.grdData, Grid.CGSQLGrid).gpWhereclause = "ClassId=" & Me._PeriodClass.ClassId

                If Me._PeriodClass.Period.DiplomaSuperTypeId = EnumDiplomaSuperTypes.PVAK Then
                    Me.grdData.gpSearchFields.Add("IdNumber")
                    Me.grdData.gpSearchFields.Add("StudentLastName")
                    Me.grdData.gpSearchFields.Add("CandidateCode")
                    Me.grdData.gpSearchFields.Add("SearchCandidateCodePVAK")

                Else
                    Me.grdData.gpSearchFields.Add("MemberNumber")
                    Me.grdData.gpSearchFields.Add("CandidateCode")
                    Me.grdData.gpSearchFields.Add("SearchCandidateCode")
                End If
                Me.grdData.Columns("IdNumber").Visible = CBool(Me._PeriodClass.Period.DiplomaSuperTypeId = EnumDiplomaSuperTypes.PVAK)
                Me.grdData.Columns("MemberNumber").Visible = Not CBool(Me._PeriodClass.Period.DiplomaSuperTypeId = EnumDiplomaSuperTypes.PVAK)

                Me.grdData.loadGrid()

            End Set
        End Property

        Private Sub frmClassMemberList_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            Me.AllowDeleteMultipleSelection = True
            Me.grdData.loadGrid()
            Do While Me.grdData.SelectedRows.Count > 0
                Me.grdData.SelectedRows(0).Selected = False
            Loop
            Me.Text = Me.Text.Replace("Class", Me.PeriodClass.Period.getClassSuffix)

            Dim r As ReportMatadata = My.Application.reportsCache.getSystemReportByKey("2")
            Me.addToReportMenu(r.DisplayName, AddressOf cmdReport1Click, r.ReportKey)

            r = My.Application.reportsCache.getSystemReportByKey("4")
            Me.addToReportMenu(r.DisplayName, AddressOf cmdReport1Click, r.ReportKey)

            r = My.Application.reportsCache.getSystemReportByKey("5")
            Me.addToReportMenu(r.DisplayName, AddressOf cmdReport1Click, r.ReportKey)

            r = My.Application.reportsCache.getSystemReportByKey("6")
            Me.addToReportMenu(r.DisplayName, AddressOf cmdReport1Click, r.ReportKey)

            r = My.Application.reportsCache.getSystemReportByKey("10")
            Me.addToReportMenu(r.DisplayName, AddressOf cmdReport1Click, r.ReportKey)

        End Sub

        Protected Sub cmdReport1Click(ByVal sender As Object, ByVal e As EventArgs)

            Dim r As ReportMatadata = My.Application.reportsCache.getSystemReportByKey(CStr(CType(sender, ToolStripMenuItem).Tag))

            Dim cr As CRReportViewer = New CRReportViewer
            cr.ReportMetaData = r
            cr.setCriterionValue("PeriodId", Me.PeriodClass.PeriodId)
            cr.setCriterionValue("LessonId", Me.PeriodClass.LessonId)
            cr.ShowReport()

        End Sub


        ''' <summary>
        ''' This function is common to all forms that inherit from class frmBaseGrid
        ''' It priovides a common name to the underlying grid control that shows the records
        ''' </summary>
        Public Overrides Function grdData() As org.codegen.win.controls.Grid.CGBaseGrid
            Return Me.UcListClassMember.grdClassMember
        End Function

        Protected Sub DeleteRecordConfirmed(ByVal sender As System.Object) Handles Me.gridDeleteRecordConfirmed

            Dim lSender As CGBaseGrid = CType(sender, CGBaseGrid)
            Dim m As New ClassMemberDBMapper

            For Each dr As DataGridViewRow In lSender.SelectedRows
                Dim pkValue As Object = lSender.IdValue
                If pkValue IsNot Nothing Then
                    Dim mo As ModelObject = m.findByKey(CInt(pkValue))
                    If mo IsNot Nothing Then
                        Call m.delete(mo)
                    End If
                End If
            Next
            Me.PeriodClass.Members = Nothing 'to force reload!
        End Sub

#End Region

        Public Overrides Sub ListEditRecord(ByVal IdValue As Integer)

            If IdValue <> 0 Then
                MyBase.ListEditRecord(IdValue)
            Else
                Dim cms As New UIClassMemberSelector()
                Call cms.selectMembers(PeriodClass)
                Me.grdData.requery()
            End If

        End Sub

        Public Overrides Function GetDeleteConfirmMsg() As String

            Dim deletedNames As List(Of String) = New List(Of String)
            For Each r As DataGridViewRow In Me.grdData.SelectedRows
                deletedNames.Add(CStr(r.Cells("StudentFirstName").Value) & " " _
                                    & CStr(r.Cells("StudentLastName").Value) & " - " _
                                    & CStr(r.Cells("CandidateCode").Value))
            Next

            If deletedNames.Count > 0 Then
                Return "The following members/students will be removed from this class/exam session:" & _
                       vbCrLf & String.Join(vbCrLf, deletedNames.ToArray) & _
                       "." & vbCrLf & "Are you sure?"
            Else
                Return String.Empty
            End If

        End Function

        Private Sub frmStudentListBase_gridSearchExecuted() Handles Me.gridSearchExecuted

            If Me.UcListClassMember.grdClassMember.RowCount = 1 _
                        AndAlso Me.GridMode = enumGridFormMode.MODE_LIST Then

                If Not Me.cmdEdit.Enabled Then Return

                Call ListEditRecord(Me.grdData.IdValue)

            End If
        End Sub
    End Class

End Namespace

